package ebiz.buddhist.texts;


import java.util.Calendar;
import ebiz.buddhist.texts.data.BowCounterHelper;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import com.nbpcorp.mobilead.sdk.MobileAdListener;
import com.nbpcorp.mobilead.sdk.MobileAdView;

public class CountRead extends Activity implements MobileAdListener {
	ArrayAdapter<String> adspin;
	Spinner mSpiner;
	BowCounterHelper mHelper;
	TextView mTextTotal;  
//	Spinner spin;
	private MobileAdView adView = null;

	Calendar cal = Calendar.getInstance();

	int currMonth = cal.get(Calendar.MONTH)+1;
	boolean firstCheck = true;
	/** Called when the activity is first created. */
	String[] arMonth = {
			"Total",
			"Jan (1月)",
			"Feb (2月)",
			"Mar (3月)",
			"Apr (4月)",
			"May (5月)",
			"Jun (6月)",	
			"Jul (7月)",
			"Aug (8月)",
			"Sep (9月)",
			"Oct (10月)",
			"Nov (11月)",
			"Dec (12月)"		
		};
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);	
	    // TODO Auto-generated method stub
	    setContentView(R.layout.bowcountread);
	    
	    
	    adView = (MobileAdView) findViewById(R.id.adview1);
	      adView.setListener(this);
		  adView.start();
		  
		  
	    mHelper = new BowCounterHelper(this);
	   
//	    String strRevIntent = revIntent();
//	    int iRevIntent = Integer.parseInt(strRevIntent.toString());
	    mSpiner = (Spinner)findViewById(R.id.bowcountspinner);
	//	spin.setPrompt(currMonth(currMonth));
	    mSpiner.setPrompt("월별 데이터");	
		mTextTotal = (TextView)findViewById(R.id.texttotal);

		adspin = new ArrayAdapter<String>(this, 
				android.R.layout.simple_spinner_item, arMonth);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpiner.setAdapter(adspin);
	   
		
		mSpiner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, 
					int position, long id) {
				if(firstCheck == true)
				{
					mSpiner.setSelection(currMonth);
					firstCheck = false;
				}			
				mOnSelect(position);				
			}
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}
	public void printList(int month)
	{
		Cursor cursor;
		//String addMonth = "0" + month;
	    SQLiteDatabase db = mHelper.getWritableDatabase();
		SimpleCursorAdapter Adapter = null;
		ListView list = (ListView)findViewById(R.id.list);

		if(month == 0)
		{
			 list.setAdapter(Adapter);
			 cursor = db.rawQuery("select * from bowcount", null);
	     
		}
		else
		{
			   list.setAdapter(Adapter);
				cursor = db.rawQuery("select * from bowcount where month="+month, null);

		}
		startManagingCursor(cursor);	
		Adapter = new SimpleCursorAdapter(this, 
		android.R.layout.simple_list_item_2,
		cursor, new String[] { "currentTime","playTime" }, 
		new int[] { android.R.id.text1, android.R.id.text2});

        list.setAdapter(Adapter);
     //   cursor.close();
     // 	mHelper.close();
	}
	public void SqlQuery(int month)
	{
	    Cursor cursor;
	    SQLiteDatabase db = mHelper.getWritableDatabase();
	    long totalTime =0,tmpTime =0;
	    int totalCount =0,tmpCount=0;
	    String strTotalTime;
	    String strPrint;

	    if(month == 0)
	    {
		    cursor = db.rawQuery("select count,realTime from bowcount", null);
	    }
	    else
	    {
	    	cursor = db.rawQuery("select count,realTime from bowcount where month="+ month, null);

	    }
	    while (cursor.moveToNext()) {
			String count = cursor.getString(0);
			String rTime = cursor.getString(1);
			
			tmpTime = Long.parseLong(rTime);
			totalTime += tmpTime;
			tmpCount = Integer.parseInt(count);
			totalCount += tmpCount;			
	    }
	    
		long minTime = (totalTime / 1000 / 60) %60;
		long hourTime = (totalTime /1000/60/60) %60;
		
		if(minTime >= 60)
			minTime -= 60;
		if(hourTime >= 24)
			hourTime -= 24;

	
	    strTotalTime = String.format("%02d일%02d시간%02d분", totalTime /1000/60/60/24,hourTime,minTime);
	    
	/*  long sum =0;
	  sum = (9*60*60*1000)+(1*24*60*60*1000);
	totalTime = totalTime - sum;
	  Date date = new Date();
	    date.setTime(totalTime);
	    SimpleDateFormat sdf = new SimpleDateFormat();
	    sdf.applyPattern("ddd일 kk시간mm분ss초");*/
	    
	    strPrint = getString(R.string.string_totalcount) + " " + totalCount + " " +
	    		getString(R.string.string_totaltime)+ " " + strTotalTime;
	    
	    mTextTotal.setText(strPrint);

	    cursor.close();
		mHelper.close();
	}
	
/*	public String revIntent()
   {
	   Intent intent = getIntent();
	   String text = intent.getStringExtra("TextIn");
	   return text;
   }*/
	public String currMonth(int month)
	{
		String setMonth;
		setMonth = month + getString(R.string.string_month);
		if(month == 0)
			setMonth = getString(R.string.string_total);
		
		return setMonth;
	}
	public void mOnSelect(int pos)
	{
		switch(pos)
		{
		case 0:
			printList(pos);
			SqlQuery(pos);
			break;
		case 1:
			printList(pos);
			SqlQuery(pos);
			break;
		case 2:
			printList(pos);
			SqlQuery(pos);
			break;
		case 3:
			printList(pos);
			SqlQuery(pos);
			break;
		case 4:
			printList(pos);
			SqlQuery(pos);
			break;
		case 5:
			printList(pos);
			SqlQuery(pos);
			break;
		case 6:
			printList(pos);
			SqlQuery(pos);
			break;
		case 7:
			printList(pos);
			SqlQuery(pos);
			break;
		case 8:
			printList(pos);
			SqlQuery(pos);
			break;
		case 9:
			printList(pos);
			SqlQuery(pos);
			break;
		case 10:
			printList(pos);
			SqlQuery(pos);
			break;
		case 11:
			printList(pos);
			SqlQuery(pos);
			break;
		case 12:
			printList(pos);
			SqlQuery(pos);
			break;
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if (adView != null) {
			adView.destroy();
			adView = null;
		}
		
	}

	public void onReceive(int err) {
		// event for receive ad 
		
	}
	

}
